import argparse
import json
import re
import os
import shutil

PLATE_CHAR = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
              "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "港", "学", "使", "警", "澳", "挂"]

parser = argparse.ArgumentParser('rec')
parser.add_argument("--result_txt", type=str, default='./plate_ocr.txt')
parser.add_argument("--imgpath", type=str, default='plate_rec_6000')
parser.add_argument("--badimgsavepath", type=str, default='E:/data/BaiduNetdiskDownload/hphm_02_0618_6000/train_hphm_rec.txt')
parser.add_argument("--isSavebadimg", type=int, default=1)
args = parser.parse_args()


def get_hphm(str):
    length = len(str)
    for i in range(length):
        if i+6<=length:
           if str[i] in PLATE_CHAR:

               if re.match(r'[A-Z0-9]{6}', str[i+1:i+7]):
                   return str[i:i+7]
               else:
                   continue
           else:
               continue
        else:
            break
    return 0

if not os.path.exists(args.badimgsavepath):
    os.mkdir(args.badimgsavepath)


f= open(args.result_txt, "r", encoding='utf-8').readlines()

r = 0
total = 0
for i in f:

    content = i.strip().split("\t")

    if len(content)>1:
        imgname = os.path.basename(content[0])
        result = content[1]
        print(result[1:7])
        if get_hphm(imgname)[1:7] == result[1:7]:
            r = r + 1
        else:
            if args.isSavebadimg:
                if os.path.exists(os.path.join(args.imgpath, imgname)):
                    shutil.copy(os.path.join(args.imgpath, imgname), os.path.join(args.badimgsavepath,imgname))

    total = total+1

print(r/float(total))
